$(function() {
	$('#information').submit(loginMethod);
	
	var name = 'GipSaveId';
	var cookies = document.cookie.split(';');
	for(var i in cookies) {
		if(cookies[i].search(name) != -1) {
			console.log('쿠키가 적용됬습니다!');
			$('#USER_ID').val(cookies[i].replace(name + '=', ''));	
			$("#keep_ID").prop("checked", true);
		}
	}
});

function loginMethod() {
	console.log( $("#keep_ID").is(":checked"));
	console.log("Login.js loginMethod() ------");
	console.log(isPermitLoginForm());
	console.log("-------------------------------");
	
	if(isPermitLoginForm()) {
		new ajaxObject('../hr/getUser.do', {	
			user_Id: $('#USER_ID').val()
		}, isMatchPassword);
	}
	else {
		alert('가입폼을 제대로 입력해주세요.');
	}
	return false;
};

function isPermitLoginForm() {
	console.log("Login.js isPermitLoginForm() ------");
	console.log($('#USER_ID').val() + " " + $('#USER_PASS').val());
	if($('#USER_ID').val() == '' || $('#USER_PASS').val() == '') 
		return false;
	return true;
};

function isMatchPassword(result) {
	
	if(!result.user) alert('존재하지 않는 사용자입니다.');
	else {
		if(result.user.user_Pass !== $('#USER_PASS').val()) alert('비밀번호가 옳지 않습니다.');
		else {
			var date = new Date();	
			
			if($("#keep_ID").is(":checked")) date.setDate(date.getDate() + 7);
			else date.setDate(date.getDate() - 1);
			
			var willCookie = '';
			willCookie += 'GipSaveId=' + $('#USER_ID').val() + ';';
			willCookie += 'expires=' + date.toUTCString();
			document.cookie = willCookie;
			
			console.log("id : " + result.user);
			new ajaxObject('../hr/loginSuccess.do', result.user , function(result) { alert('로그인 완료'); });
		
			location.href = "../../Group/html/group_enroll.html";
			
			
		}
	}
}